// 各页面以组件形式 以组件活跃标识标记
import Api from '@/services/api.js'
const app = getApp();
const options = [{
  title: '完全认同',
  isCheck: false,
  grades: 2

}, {
  title: '一般认同',
  isCheck: false,
  grades: 1
}, {
  title: '不太认同',
  isCheck: false,
  grades: 0
}]
export default {
  components: {
  },
  data() {
    return {
      PRIMARY_TEXT_COLOR: app.globalData.PRIMARY_TEXT_COLOR,
      list: [], // 题库列表
      currItem: {}, // 当前的题目
      currIndex: 0, // 当前题目的角标

    }
  },
  computed: {
    // 是否是最后一题
    isLastTopic() {
      return this.currIndex == (this.list.length - 1);
    },
    // 是否有选项被选中
    hasOptionItemChecked() {
      return (this.currItem?.options || []).some(i => i.isCheck);
    },
    // 答题者信息
    answererInfo() {
      return this.$store.state.answererInfo;
    },
    // companyInfo() {
    //   return this.$store.state.companyInfo;
    // }
  },
  created() {
    this.getAllTopic();
  },
  mounted() {
  },
  methods: {
    // 获取所有题目
    async getAllTopic() {
      const ret = await this.$request({ 
        url: Api.getAllTopic,
        data: {
          isTopManager: this.answererInfo.jobTag // 如果是高层 传2 获取72题
        }
      })
      if (ret.code == 200) {
        this.list = ret.result.map(item => {
          item.options = options;
          return item;
        })
        this.currItem = this.list[0];
      }
    },
    handleOption(e) {
      const { parentItem, childItem, childIdx } = e.currentTarget.dataset;
      // 处理父角标下的数据
      parentItem.options.map((item, idx) => {
        item.isCheck = (idx == childIdx);
        if (idx == childIdx) {
          item.mark = childItem.grades;
          item.topicId = parentItem.id;
          item.num = parentItem.num;
          item.serialNumId = parentItem.serialNumId;
        }
      })
      // 更新处理后的数据
      this.currItem = parentItem;
      this.list[this.currIndex] = parentItem;
    },
    // 上一题/下一题 
    handleTap(e) {
      const { type } = e.currentTarget.dataset;
      switch (type) {
        case 'prev':
          this.currIndex--;
          this.currItem = this.list[this.currIndex];
          break;
        case 'next':
          if (!this.hasOptionItemChecked) {
            return uni.showToast({ title: '请选择一个答案', icon: 'none' });
          }
          this.currIndex++;
          this.currItem = this.list[this.currIndex];
          break;
        case 'submit':
          const arr = this.list.reduce((prev, next) => {
            const optItem = next.options.find(i => i.isCheck);
            if (optItem) {
              prev.push(optItem)
            }
            return prev;
          }, [])

          const params = {
            answers: arr,
            ...this.answererInfo,
            // enterpriseId: this.companyInfo.enterpriseId
            // departmentId: "1840570789563002881",
            // jobId: "1730106377870ikWk",
            // enterpiseId: "1727661247022jPMFBIRt",
            // newOrOld: "0",
            // userName: "李伟",
            // userSex: "1",
            // userAge: "39",
            // joinDate: ""
          }
          this.requestSubmit(params);
          break;
      }
    },
    // 请求提交
    async requestSubmit(params) {
      console.log(params)
      const ret = await this.$request({
        url: Api.postAllAnswer,
        method: 'POST',
        data: params
      })
      if (ret) {
        uni.showToast({ title: '提交成功', icon: 'none' })
        setTimeout(() => {
          uni.reLaunch({
            url: '/pages/home/index'
          })
        }, 800)
      }
    }
  }
}